package com.oocl.crm.util;

import com.oocl.crm.exception.ParameterErrorException;

public class StringUtil {
	
	public static String addHelpDoc = "A field:value[,field:value]  \n"+
		"--- Use A command you can add a user.  \n"+
		"--- For example: A name:abel,sex:1,birthday:19950422,address:ZHA,call:110 \n";
	public static String deleteHelpDoc = "D id  \n"+
		"--- Use D command you can delete a user.  \n"+
		"--- For example: D 1 \n";
	public static String updateHelpDoc = "U id field:value[,field:value]  \n"+
		"--- Use U command you can update a user info.  \n"+
		"--- For example: U 1 name:abel,sex:0 \n";
	public static String lookupHelpDoc = "L [field:value]  \n"+
		"--- Use L command you can loopup userlist.  \n"+
		"--- For example: L sex:1 \n";
	public static String orderHelpDoc = "O [id/name]  \n"+
		"--- Use O command you can order by userlist.  \n"+
		"--- For example: O id \n";
	public static String helpHelpDoc = "H   \n"+
		"--- Use H call out help doc.  \n"+
		"--- For example: H \n";
	public static String quitHelpDoc = "Q \n"+
		"--- Use O command you can quit this system. \n"+
		"--- For example: Q \n";

	/**
	 * 分割第一个参数和后续参数，返回长度为2的数组。
	 * @param params
	 * @return
	 * @throws ParameterErrorException 
	 */
	public static String[] split(String params) throws ParameterErrorException{
		if(params != null){
			String[] temps = params.split(" ");
			//超过2个空格，则取出第一个为ID，后面合并并且去除两边空格。
			if(temps.length > 2){
				String command = temps[0];
				String fieldStr = "";
				for(int i = 1; i < temps.length; i++){
					fieldStr= fieldStr + temps[i] + " ";
				}
				//清除两边空格
				fieldStr.trim();
				return new String[]{command, fieldStr};
			}else if(temps.length == 2){
				return temps;
			}else{
				String command = temps[0];
				return new String[]{command, ""};
			}
		}
		throw new ParameterErrorException("parameter split exception: parameter does not conform to the format! ");
	}
	
	public static String helpDoc(){
		String helpText = "";
		helpText+="-----------------------------------------------------------\n";
		helpText+="Welcome use Crm System![Author by Abel]\n";
		helpText+="-----------------------------------------------------------\n";
		helpText+="Help doc: \n";
		helpText+=addHelpDoc;
		helpText+=deleteHelpDoc;
		helpText+=updateHelpDoc;
		helpText+=lookupHelpDoc;
		helpText+=orderHelpDoc;
		helpText+=helpHelpDoc;
		helpText+=quitHelpDoc;
		helpText+="-----------------------------------------------------------\n";
		return helpText;
	}
}
